<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class FeedbackModel extends AppModel
{

	var $tbl_pref_custom = '';
	var $tables = array('table'=>'feedback','user');
	var $custom_tables = array('category'=>'kb_category');
	
	
	function getById($record_id) {
		$this->setSqlParams("AND e.id = '$record_id'");
		$sql = $this->getRecordsSql();
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->FetchRow();
	}

	
	function getRecordsSql() {
		
		$sql = "
		SELECT
			e.*,		
			u.username,
			u.first_name,
			u.last_name,
			#IFNULL(u.username, e.name) AS name,
			IFNULL(u.email, e.email) AS email,
			cat.name AS category_name,
			UNIX_TIMESTAMP(e.date_posted) AS ts
		
		FROM 
			{$this->tbl->table} e
		LEFT JOIN {$this->tbl->category} cat ON e.category_id = cat.id
		LEFT JOIN {$this->tbl->user} u ON e.user_id = u.id
		
		WHERE {$this->sql_params}
		{$this->sql_params_order}";
		
		//echo "<pre>"; print_r($sql); echo "</pre>";
		return $sql;
	}
	
	
	// ACTIONS // ---------------------
	
	function setEntryAnswered($record_id, $values) {
		$sql = "UPDATE {$this->tbl->table} SET 
		answer = '{$values[answer]}',
		admin_id = '{$values[admin_id]}',
		date_answered = NOW(),
		answered = 1 
		WHERE id = '{$record_id}'";
		$this->db->Execute($sql) or die(db_error($sql));
	}
	
	
	function sendAnswer($vars) {
		
		require_once 'core/app/AppMailSender.php';
		
		$sender = &new AppMailSender();
		$sender->letter_key = 'answer_to_user';

		// parser
		$parser = &$sender->parser;
		$parser->assign($vars);
		
		// mail object
		$mail = &$sender->getMailerObj($sender->getTemplateVars());
		$mail->Body = &$vars['answer'];
		
		//echo "<pre>"; print_r($mail); echo "</pre>"; 
		//exit();	
		
		return $mail->Send();
	}	
	
	
	function sendFileDownload($data, $file_num) {
		
		require_once 'HTTP/Download.php';
		PEAR::setErrorHandling(PEAR_ERROR_PRINT);
		
		session_write_close();
		ini_set('zlib.output_compression', 'Off');
		
		$files = explode(';', $data['attachment']);
		$file = $files[$file_num];
		$filename = basename($files[$file_num]);
		
		$params['file'] = $file;
		$params['gzip'] = true;
		
		//echo "<pre>"; print_r($params); echo "</pre>";
		//echo "<pre>"; print_r($filename); echo "</pre>";
		//exit;
		
		$h = &new HTTP_Download($params);
        $h->setContentDisposition(HTTP_DOWNLOAD_ATTACHMENT, $filename); //HTTP_DOWNLOAD_INLINE
		$h->send();
	}	
	
	
	
	// OTHER // ------------------------------
	
	// if check priv is different for model so reassign
	function checkPriv(&$priv, $action, $record_id) {
	
		$priv->setCustomAction('answer', 'update');
		$priv->setCustomAction('file', 'update');
		$priv->setCustomAction('answer_status', 'status');
		$priv->setCustomAction('place_status', 'status');
		$priv->check($action);
	}
	
	
	// DELETE RELATED // --------------------- 

	
	function getAttachment($id) {
		$sql = "SELECT attachment FROM {$this->tbl->table} WHERE id = '$id'";
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->Fields('attachment');
	}
	
	function deleteFilesFromDisk($attachment) {
		if($attachment) {
			$files = explode(';', $attachment);
			foreach($files as $k => $file) {
				unlink($file);
			}		
		}
	}
	
	function delete($id) {
		$attachment = $this->getAttachment($id);
		parent::delete($id);
		$this->deleteFilesFromDisk($attachment);
	}
}
?>
